package com.hjq.demo.yuliao.fragment

import android.annotation.SuppressLint
import android.view.*
import com.bumptech.glide.Glide
import com.dylanc.mmkv.MMKVOwner
import com.gyf.immersionbar.ImmersionBar
import com.hjq.demo.R
import com.hjq.demo.app.vb.AppVbActivity
import com.hjq.demo.app.vb.TitleBarVbFragment
import com.hjq.demo.databinding.FragmentMineHomeMainBinding
import com.hjq.demo.http.model.HttpData
import com.hjq.demo.yuliao.activity.BackPackActivity
import com.hjq.demo.yuliao.activity.SettingsMyActivity
import com.hjq.demo.yuliao.activity.UserinfoActivity
import com.hjq.demo.yuliao.activity.WalletActivity
import com.hjq.demo.yuliao.api.GetUserinfoApi
import com.hjq.http.EasyHttp
import com.hjq.http.listener.HttpCallback
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 *
 * @author jiahe
 * 创建日期 2022/2/8
 * 描述 首页个人中心
 *
 */
class HomeMainMineFragment : TitleBarVbFragment<AppVbActivity<*>, FragmentMineHomeMainBinding>(), OnRefreshListener,
    MMKVOwner {

    companion object {

        fun newInstance(): HomeMainMineFragment {
            return HomeMainMineFragment()
        }
    }


    override fun initView() {
        ImmersionBar.setTitleBar(this, binding.llMineHead)
        binding.rlStatusRefresh.setOnRefreshListener(this)
        binding.rlStatusRefresh.setEnableLoadMore(false)

        setOnClickListener(
            binding.dtvWallet, binding.dtvBackpack, binding.sbSetting,
            binding.ivUserHead, binding.llUserinfo, binding.tvUserinfo,
            binding.sbTest
        )
    }

    override fun initData() {
        getUserData()
    }

    override fun isStatusBarEnabled(): Boolean {
        return !super.isStatusBarEnabled()
    }

    override fun onClick(view: View) {
        super.onClick(view)

        if (view == binding.dtvWallet) {
            getAttachActivity()?.let { WalletActivity.start(it, "") }
            return
        }
        if (view == binding.dtvBackpack) {
            getAttachActivity()?.let { BackPackActivity.start(it, "") }
            return
        }
        if (view == binding.sbSetting) {
            getAttachActivity()?.let { SettingsMyActivity.start(it, "") }
            return
        }
        if (view == binding.llUserinfo || view == binding.ivUserHead || view == binding.tvUserinfo) {
            getAttachActivity()?.let { UserinfoActivity.start(it, "") }
            return
        }
        if (view == binding.sbTest) {
//            IntentWrap.launchRoom(requireContext(), voiceRoomBean.getRoomType(), voiceRoomBean.getRoomId())

//            RouteUtils.routeToConversationActivity(context, Conversation.ConversationType.PRIVATE, "17");

//            getAttachActivity()?.let {
//                MaiWeiDialog.Builder(it)
//                    .setTitle("123")
//                    .setDes("456")
//                    .setListener(object : MaiWeiDialog.Builder.OnListener{
//                        override fun onAttention(dialog: BaseDialog?) {
//                            toast("attention")
//                        }
//
//                        override fun onSiXiin(dialog: BaseDialog?) {
//
//                        }
//
//                        override fun onGift(dialog: BaseDialog?) {
//                            toast("gift")
//                        }
//
//                    })
//                    .show()
//            }
        }
    }

    /**
     * [OnRefreshLoadMoreListener]
     */
    override fun onRefresh(refreshLayout: RefreshLayout) {
        getUserData()
        postDelayed({
            binding.rlStatusRefresh.finishRefresh()
        }, 1000)
    }

    /**
     * 用户信息
     */
    private fun getUserData() {
        EasyHttp.post(this)
            .api(GetUserinfoApi())
            .request(object : HttpCallback<HttpData<GetUserinfoApi.Response?>>(this) {
                @SuppressLint("SetTextI18n")
                override fun onSucceed(result: HttpData<GetUserinfoApi.Response?>?) {

                    kv.encode("userinfo", result?.getData()?.user_info)

                    // 头像
                    Glide.with(this@HomeMainMineFragment)
                        .load(result?.getData()?.user_info?.avatar_url)
                        .placeholder(R.drawable.avatar_placeholder_ic)
                        .error(R.drawable.avatar_placeholder_ic)
                        .circleCrop()
                        .into(binding.ivUserHead)
                    // 用户名
                    binding.tvUsername.text = result?.getData()?.user_info?.name
                    binding.tvUserId.text = "ID: " + result?.getData()?.user_info?.user_id
                    binding.tvFansNum.text = result?.getData()?.user_info?.fans_count
                    binding.tvAttentionNum.text = result?.getData()?.user_info?.attention_count

                }
            })
    }

}